﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using T.REST;

namespace T.Rest.Test
{
    [TestClass]
    public class DemoTests
    {
        [TestInitialize]
        public void TestInitialize()
        {
            RessourceFactory.Init(Assert.Fail, Assert.AreEqual, Assert.AreNotEqual, Assert.AreEqual, Assert.AreNotEqual);
        }

        [TestMethod]
        public void JqueryUIDemo()
        {
            var expected = new Dictionary<string, Type>
                               {
                                   {"latitude", typeof (decimal)},
                                   {"longitude", typeof (decimal)},
                                   {"title", typeof(string)},
                                   {"content", typeof(string)}
                               };

            var SUT = RessourceFactory.Create(new RestConfiguration
                                                  {
                                                      Url = "/svn/trunk/demos/json/demo.json",
                                                      Host = "jquery-ui-map.googlecode.com",
                                                      ExpectedObjectSignature = expected,
                                                      ExpectArrayResult = true
                                                  });
            SUT.ValidateSignature();
        }

        [TestMethod]
        public void JqueryUIDemo_DeserializeToObject()
        {
            var res = RessourceFactory.Create(new RestConfiguration
            {
                Url = "/svn/trunk/demos/json/demo.json",
                Host = "jquery-ui-map.googlecode.com",
                ExpectArrayResult = true
            });

            var request = new ResRequest<GeoLocations>(res);
            var obj = request.Parse((sz, str) => sz.Deserialize<GeoLocations>(str));

            Assert.AreNotEqual(null, obj.Markers);     
            Assert.AreEqual(2, obj.Markers.Count);
        }

        private class GeoLocations
        {
            public List<GeoLocation> Markers { get; set; }
        }

        private class GeoLocation
        {
            public decimal Latitude { get; set; }
            public decimal Longitude { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }
        }

        [TestMethod]
        [Ignore]
        public void SubscribeToNewsletter_VerifyCallExecution()
        {
            var res = RessourceFactory.Create(new RestConfiguration
                                                  {
                                                      Host = "my.restapi.net",
                                                      Url = "/newsletter/subscribe",
                                                      Method = "POST",
                                                      FormsCollection = new NameValueCollection
                                                                            {
                                                                                {"name", "John Doe"},
                                                                                {"email", "john@doe.com"}
                                                                            },                                                      
                                                  });
            var request = new ResRequest(res);
            request.Execute();

            var expectedResult = new Dictionary<string, Type>
                                     {
                                         {"email", typeof (string)},
                                         {"subscribed", typeof (bool)}
                                     };
            res = RessourceFactory.Create(new RestConfiguration
            {
                Host = "my.restapi.net",
                Url = "/newsletter/subscriptions/john%40doe.com",
                ExpectedObjectSignature = expectedResult
            });
            res.ValidateSignature();
        }
    }
}
